package com.sita.linboard.wallet;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.sita.linboard.R;
import com.sita.linboard.base.BaseActivity;
import com.sita.linboard.base.BaseApplication;
import com.sita.linboard.http.RestClient;
import com.sita.linboard.utils.SpUtils;
import com.sita.linboard.utils.ToastUtils;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by Jason on 2016/9/27.
 */
public class WithdrawalActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    private SwipeRefreshLayout swipeRefreshLayout;
    private ListView withdrawaListView;
    private WithdrawaAdapter adapter;
    private LinearLayout back;
    private TextView head;
    @Override
    protected void initData() {
        withdrawaListView.setAdapter(adapter);
        swipeRefreshLayout.setOnRefreshListener(this);
        getData();
    }
    private void getData(){
        BalanceRequest request = new BalanceRequest();
        request.driver_id = SpUtils.getString("AccoundID",null,BaseApplication.getContext());
        RestClient.getRestService().withdrawal(request, new Callback<WithdrawBackBean>() {
            @Override
            public void success(WithdrawBackBean withdrawBackBean, Response response) {
                if (withdrawBackBean.getErrorCode().equals("0")&&withdrawBackBean.getData()!=null){
                    adapter.setData(withdrawBackBean.getData());
                }
                else {
                    ToastUtils.show("提现记录获取失败",3);
                    return;
                }

            }

            @Override
            public void failure(RetrofitError error) {
                ToastUtils.show("提现记录获取失败",3);
                return;
            }
        });
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        withdrawaListView  = bindView(R.id.withdrawal);
        adapter = new WithdrawaAdapter(this);
        back = bindView(R.id.img_back);
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        head = bindView(R.id.head_title);
        head.setText("提现记录");
        head.setVisibility(View.VISIBLE);
        withdrawaListView.addFooterView(LayoutInflater.from(this).inflate(R.layout.listivew_foot,null));
        swipeRefreshLayout = bindView(R.id.list_refresh);

    }

    @Override
    protected int getId() {
        return R.layout.activity_withdrawal ;
    }
    public static void WithDrawalIntent(Context context){
        Intent intent = new Intent(context,WithdrawalActivity.class);
        context.startActivity(intent);
    }

    @Override
    public void onRefresh() {
        getData();
        swipeRefreshLayout.setRefreshing(false);
    }
}
